from flask import Flask, request, jsonify, render_template_string
import os

app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = 'uploads'
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024  # 16MB max file size

# 确保上传目录存在
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)

@app.route('/')
def index():
    html_content = '''
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Python文件上传演示</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 600px;
            margin: 50px auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background: white;
            padding: 40px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            color: #333;
            text-align: center;
            margin-bottom: 30px;
        }
        .upload-area {
            border: 2px dashed #ddd;
            border-radius: 5px;
            padding: 40px;
            text-align: center;
            transition: border-color 0.3s;
        }
        .upload-area:hover {
            border-color: #007bff;
        }
        .file-input {
            display: none;
        }
        .upload-btn {
            background: #007bff;
            color: white;
            padding: 12px 30px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
            margin: 10px;
        }
        .upload-btn:hover {
            background: #0056b3;
        }
        .file-info {
            margin-top: 20px;
            padding: 15px;
            background: #f8f9fa;
            border-radius: 5px;
            display: none;
        }
        .success-message {
            color: #28a745;
            font-weight: bold;
            margin-top: 10px;
            display: none;
        }
        .error-message {
            color: #dc3545;
            font-weight: bold;
            margin-top: 10px;
            display: none;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>Python文件上传演示</h1>
        <div class="upload-area">
            <input type="file" id="fileInput" class="file-input" accept=".py" />
            <button class="upload-btn" onclick="document.getElementById('fileInput').click()">
                选择Python文件
            </button>
            <button class="upload-btn" id="uploadBtn" style="display: none;" onclick="uploadFile()">
                上传文件
            </button>
            <div id="fileInfo" class="file-info"></div>
            <div id="successMessage" class="success-message"></div>
            <div id="errorMessage" class="error-message"></div>
        </div>
    </div>

    <script>
        const fileInput = document.getElementById('fileInput');
        const uploadBtn = document.getElementById('uploadBtn');
        const fileInfo = document.getElementById('fileInfo');
        const successMessage = document.getElementById('successMessage');
        const errorMessage = document.getElementById('errorMessage');

        fileInput.addEventListener('change', function(e) {
            const file = e.target.files[0];
            if (file) {
                // 检查文件扩展名
                if (!file.name.endsWith('.py')) {
                    errorMessage.textContent = '请选择Python文件（.py）';
                    errorMessage.style.display = 'block';
                    uploadBtn.style.display = 'none';
                    fileInfo.style.display = 'none';
                    return;
                }
                
                fileInfo.innerHTML = `
                    <strong>文件名：</strong>${file.name}<br>
                    <strong>文件大小：</strong>${(file.size / 1024).toFixed(2)} KB<br>
                    <strong>文件类型：</strong>${file.type || 'text/x-python'}
                `;
                fileInfo.style.display = 'block';
                uploadBtn.style.display = 'inline-block';
                errorMessage.style.display = 'none';
            }
        });

        function uploadFile() {
            const file = fileInput.files[0];
            if (!